#include <stdio.h>

struct student {
    int num;
    char name[20];
    int score;
};

void Input(struct student stud[], int n);

void Output(struct student stud[], int n);

void Sort(struct student stud[], int n);

double Avg(struct student stud[], int n);

int main(void) {
    int n;
    struct student stud[10];  /* 定义结构数组 */

    printf("请输入学生个数n的值: ");
    scanf("%d", &n);
    Input(stud, n);
    Sort(stud, n);
    Output(stud, n);
    printf("平均分: %.2f\n", Avg(stud, n));
    return 0;
}

void Input(struct student stud[], int n) {
    int i;
    for (i = 0; i < n; i++) {
        printf("请输入第%d个学生的学号、姓名和成绩: ", i + 1);
        scanf("%d%s%d", &stud[i].num, stud[i].name,
              &stud[i].score);

    }
}

void Sort(struct student stud[], int n) {
    int i, j;
    struct student temp;

    /* 按照分数从低到高排序*/
    for (i = 0; i < n - 1; ++i) {
        for (j = i + 1; j < n; j++)
            if (stud[j].score > stud[i].score)  /* 比较成绩 */
            {
                /*交换数组元素*/
                temp = stud[j];
                stud[j] = stud[i];
                stud[i] = temp;
            }
    }
}

double Avg(struct student stud[], int n) {
    int i;
    double sum = 0;
    for (i = 0; i < n; i++)
        sum = sum + stud[i].score;
    return sum / n;
}

void Output(struct student stud[], int n) {
    int i;
    printf("学生列表信息如下:\n");
    printf("Num\tName\tScore\n");
    for (i = 0; i < n; i++) {
        printf("%d\t%s\t%d\n", stud[i].num, stud[i].name, stud[i].score);
    }
    printf("\n");
}